// pages/bound/bound.js
var util = require('../../../utils/util.js')
var interval = null //倒计时函数
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '发送验证码', 
    currentTime: 60,//倒计时 

    inputValue: '',
    pwsValue: "",
    boolean: true,
    focus:false
  },
  clearInput: function () {
    this.setData({ inputValue: '' })
  },
  EventHandle: function () {
    var bol = this.data.boolean;
    this.setData({
      boolean: !bol,
      focus: true,
    })

  },
  goLogin: function () {
    wx.navigateBack({
      url: "../my/my"
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var userid = wx.getStorageSync('UserID') || []
    if(userid==""){ //如果没有获取到userid，就返回我的
      wx.navigateBack({
        delta: 1
      })
    }

    var that = this;
    wx.getStorage({ //加载头像
      key: 'HeadImg',
      success: function (res) {
        that.setData({ HeadImg: res.data });
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getMobile:function(e){    //监听手机号
    var mobile = e.detail.value;
    this.setData({ Mobile:""});
    this.setData({Mobile:mobile});
  },
  getSMSCode: function (e) {    //监听验证码
    var smscode = e.detail.value;
    this.setData({ SMSCode: "" });
    this.setData({ SMSCode: smscode });
  },
  getUserPwd: function (e) {    //监听密码
    var userpwd = e.detail.value;
    this.setData({ UserPwd: "" });
    this.setData({ UserPwd: userpwd });

  },
  SendSMS:function(){ //发送验证码
    var that = this;    
    var mobile = that.data.Mobile;
    if(mobile != undefined){
      if (mobile.length == 11 && mobile > 10000000000) {  //手机号验证成功

        var that = this;
        var data = {
          mobile : mobile
        };
        data.act = "Send_IdentifySmsCode";
        util.Ajax(data, function (json) {
          console.log(json);
          if (json.state) {
            util.ShowSuccess("发送成功");
            that.getCode();
            that.setData({
              disabled: true
            })
          }else{
            util.ShowError(json.msg);
          }
        });        
      } else {
        util.ShowError("手机号格式错误");
      }
    }else{
      util.ShowError("请输入手机号");
    }
  },    
 getCode: function () { //设置时间提示
   var that = this;
   var currentTime = that.data.currentTime
   interval = setInterval(function () {
     currentTime--;
     that.setData({
       title: currentTime + 's后重新发送'
     })
     if (currentTime <= 0) {
       clearInterval(interval)
       that.setData({
         title: '发送验证码',
         currentTime: 60,
         disabled: false
       })
     }
   }, 1000)
 },
 BindingUserInfo:function(){  //绑定用户信息
   var that = this;
   var mobile = that.data.Mobile;
   var smscode = that.data.SMSCode;
   var userpwd = that.data.UserPwd;
   var uid = wx.getStorageSync('UserID') || []//获取本地储存的 用户信息
   if (mobile == undefined ){
     util.ShowError("请输入手机号");
     return;
   }
   if (mobile.length != 11 || mobile <= 10000000000) {
     util.ShowError("手机号格式错误");
     return
   }
   if (smscode == undefined) {
     util.ShowError("请输入验证码");
     return;
   }
   if (userpwd == undefined) {
     util.ShowError("请输入密码");
     return;
   }
   var data = {
     id : uid,
     mobile : mobile,
     smscode : smscode,
     userpwd : userpwd
   }
   data.act = "Binding_User";
   util.Ajax(data, function (json) {
     if (json.state) {
       util.ShowSuccess(json.msg);   //绑定成功 更换显示文字

       var pages = getCurrentPages();
       var prevPage = pages[pages.length - 2];
       var title = "已绑定手机号";
       var text = mobile;          
       prevPage.setData({ "myList[0].myTitle": title, "myList[0].text": text, "myList[0].url": "" });       

       setTimeout(function () {
         wx.navigateBack({
           delta: 2
         })
       }, 1500);
       
     } else {
       util.ShowError(json.msg);
     }
   });        

 }

})